home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / module.inc < prev    next >
Encoding:
Text File  |  2005-04-03  |  6.0 KB  |  219 lines

  1. <?php
  2. // $Id: module.inc,v 1.67 2005/04/03 08:03:18 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * API for loading and interacting with Drupal modules.
  7.  */
  8.  
  9. /**
  10.  * Initialize all modules.
  11.  *
  12.  */
  13. function module_init() {
  14.   module_load_all();
  15.   module_invoke_all('init');
  16. }
  17.  
  18. /**
  19.  * Call a function repeatedly with each module in turn as an argument.
  20.  */
  21. function module_iterate($function, $argument = '') {
  22.   foreach (module_list() as $name) {
  23.     $function($name, $argument);
  24.   }
  25. }
  26.  
  27. /**
  28.  * Collect a list of all loaded modules. During the bootstrap, return only
  29.  * vital modules. See bootstrap.inc
  30.  *
  31.  * @param $refresh
  32.  *   Whether to force the module list to be regenerated (such as after the
  33.  *   administrator has changed the system settings).
  34.  * @param $bootstrap
  35.  *   Whether to return the reduced set of modules loaded in "bootstrap mode"
  36.  *   for cached pages. See bootstrap.inc.
  37.  * @return
  38.  *   An associative array whose keys and values are the names of all loaded
  39.  *   modules.
  40.  */
  41. function module_list($refresh = FALSE, $bootstrap = TRUE) {
  42.   static $list;
  43.  
  44.   if ($refresh) {
  45.     $list = array();
  46.   }
  47.  
  48.   if (!$list) {
  49.     $list = array('filter' => 'filter', 'system' => 'system', 'user' => 'user', 'watchdog' => 'watchdog');
  50.     if ($bootstrap) {
  51.       $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1");
  52.     }
  53.     else {
  54.       $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1");
  55.     }
  56.     while ($module = db_fetch_object($result)) {
  57.       if (file_exists($module->filename)) {
  58.         // Determine the current throttle status and see if the module should be
  59.         // loaded based on server load. We have to directly access the throttle
  60.         // variables, since throttle.module may not be loaded yet.
  61.         $throttle = ($module->throttle && variable_get('throttle_level', 0) > 0);
  62.         if (!$throttle) {
  63.           drupal_get_filename('module', $module->name, $module->filename);
  64.           $list[$module->name] = $module->name;
  65.         }
  66.       }
  67.     }
  68.     asort($list);
  69.   }
  70.   return $list;
  71. }
  72.  
  73. /**
  74.  * Load all the modules that have been enabled in the system table.
  75.  *
  76.  * @return
  77.  *   TRUE if all modules were loaded successfully.
  78.  */
  79. function module_load_all() {
  80.   $list = module_list(TRUE, FALSE);
  81.   $status = TRUE;
  82.  
  83.   foreach ($list as $module) {
  84.     $status = (drupal_load('module', $module) && $status);
  85.   }
  86.  
  87.   return $status;
  88. }
  89.  
  90.  
  91. /**
  92.  * Determine whether a given module exists.
  93.  *
  94.  * @param $module
  95.  *   The name of the module (without the .module extension).
  96.  * @return
  97.  *   TRUE if the module is both installed and enabled.
  98.  */
  99. function module_exist($module) {
  100.   $list = module_list();
  101.   return array_key_exists($module, $list);
  102. }
  103.  
  104. /**
  105.  * @defgroup hooks Hooks
  106.  * @{
  107.  * Allow modules to interact with the Drupal core.
  108.  *
  109.  * Drupal's module system is based on the concept of "hooks". A hook is a PHP
  110.  * function that is named foo_bar(), where "foo" is the name of the module (whose
  111.  * filename is thus foo.module) and "bar" is the name of the hook. Each hook has
  112.  * a defined set of parameters and a specified result type.
  113.  *
  114.  * To extend Drupal, a module need simply implement a hook. When Drupal wishes to
  115.  * allow intervention from modules, it determines which modules implement a hook
  116.  * and call that hook in all enabled modules that implement it.
  117.  *
  118.  * The available hooks to implement are explained here in the Hooks section of
  119.  * the developer documentation. The string "hook" is used as a placeholder for
  120.  * the module name is the hook definitions. For example, if the module file is
  121.  * called example.module, then hook_help() as implemented by that module would be
  122.  * defined as example_help().
  123.  */
  124.  
  125. /**
  126.  * Determine whether a module implements a hook.
  127.  *
  128.  * @param $module
  129.  *   The name of the module (without the .module extension).
  130.  * @param $hook
  131.  *   The name of the hook (e.g. "help" or "menu").
  132.  * @return
  133.  *   TRUE if the module is both installed and enabled, and the hook is
  134.  *   implemented in that module.
  135.  */
  136. function module_hook($module, $hook) {
  137.   return function_exists($module .'_'. $hook);
  138. }
  139.  
  140. /**
  141.  * Determine which modules are implementing a hook.
  142.  *
  143.  * @param $hook
  144.  *   The name of the hook (e.g. "help" or "menu").
  145.  * @return
  146.  *   An array with the names of the modules which are implementing this hook.
  147.  */
  148. function module_implements($hook) {
  149.   static $implementations;
  150.  
  151.   if (!isset($implementations[$hook])) {
  152.     $implementations[$hook] = array();
  153.     $list = module_list();
  154.     foreach ($list as $module) {
  155.       if (module_hook($module, $hook)) {
  156.         $implementations[$hook][] = $module;
  157.       }
  158.     }
  159.   }
  160.  
  161.   return $implementations[$hook];
  162. }
  163.  
  164. /**
  165.  * Invoke a hook in a particular module.
  166.  *
  167.  * @param $module
  168.  *   The name of the module (without the .module extension).
  169.  * @param $hook
  170.  *   The name of the hook to invoke.
  171.  * @param ...
  172.  *   Arguments to pass to the hook implementation.
  173.  * @return
  174.  *   The return value of the hook implementation.
  175.  */
  176. function module_invoke() {
  177.   $args = func_get_args();
  178.   $module = array_shift($args);
  179.   $hook = array_shift($args);
  180.   $function = $module .'_'. $hook;
  181.   if (module_hook($module, $hook)) {
  182.     return call_user_func_array($function, $args);
  183.   }
  184. }
  185. /**
  186.  * Invoke a hook in all enabled modules that implement it.
  187.  *
  188.  * @param $hook
  189.  *   The name of the hook to invoke.
  190.  * @param ...
  191.  *   Arguments to pass to the hook.
  192.  * @return
  193.  *   An array of return values of the hook implementations. If modules return
  194.  *   arrays from their implementations, those are merged into one array.
  195.  */
  196. function module_invoke_all() {
  197.   $args = func_get_args();
  198.   $hook = array_shift($args);
  199.   $return = array();
  200.   foreach (module_implements($hook) as $module) {
  201.     $function = $module .'_'. $hook;
  202.     $result = call_user_func_array($function, $args);
  203.     if (is_array($result)) {
  204.       $return = array_merge($return, $result);
  205.     }
  206.     else if (isset($result)) {
  207.       $return[] = $result;
  208.     }
  209.   }
  210.  
  211.   return $return;
  212. }
  213.  
  214. /**
  215.  * @} End of "defgroup hooks".
  216.  */
  217.  
  218. ?>
  219.